﻿#include "pch.h"
#include <vector>
#include "Pipe.h"
#include "PropertyData.h"
#include "PipeFunc.h"
#include "msword.h"
//#include "Water.h"
//#include <windows.h>
#include "refprop1.h"
#include <string>

IMPLEMENT_SERIAL(CPipe, CObject, 1)

CPipe::CPipe()
	:t(0)
	,p(0)
	,flowRate(0)
	,preV(0)
	//,preDim(0)
	,td(0)
	,pd(0)
	,di(0)
	,dout(0)
	,length(0)
	,nRoughId(-1)
	,rough(0)
	,localCoef(0)
	,insulThk(0)
	,nShellId(-1)
	,blackness(0)
	,nBracketNum(0)
	,bracketLength(0)
	,bracketHeight(0)
	,nPlaceId(0)
	,ta(0)
	,windV(0)
	,allowStress(0)
	,nWeldingType(0)
	,weldingRTPercent(0)
	,weldingCoef(1)
	,c1(12.5)
	,c2(0.75)
	,c3(0)
{
	//strcpy_s(components[0], sizeof components[0] / sizeof components[0][0], "water.fld");
	//strcpy_s(components[1], sizeof components[1] / sizeof components[1][0], "H2S");
	ncomp = 0;
	//x[0] = 1;
	//x[1] = 0.9;
	for (int i = 0; i < ncmax; i++)
	{
		x[i] = 0;
	}
}

CPipe::~CPipe()
{

}

void CPipe::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{	// storing code
		ar << t << p << flowRate << preV << /*preDim <<*/ di << dout << length << td << pd << rough
			<< nRoughId << nInsulId << nShellId; 
		size_t nCount = localIdList.size();
		ar << nCount;
		for (int i : localIdList)
		{
			ar << i;
		}
		for (auto n : localNumList)
		{
			ar << n;
		}
		for (auto k : localKList)
		{
			ar << k;
		}
		ar << localCoef << insulThk << blackness << insulName << city << nBracketNum << nPlaceId
			<< bracketLength << bracketHeight << windV << ta << nPipeId << nWeldingType
			<< allowStress << weldingRTPercent << weldingCoef << c1 << c2 << c3;
		
		ar << ncomp;
		for (int i = 0; i < ncomp; i++)
		{
			ar << CString(components[i]);
		}
		for (int i = 0; i < ncomp; i++)
		{
			ar << x[i];
		}
	}
	else
	{	// loading code
		ar >> t >> p >> flowRate >> preV >> /*preDim >>*/ di >> dout >> length >> td >> pd >> rough
			>> nRoughId >> nInsulId >> nShellId;
		size_t nCount;
		ar >> nCount;
		for (size_t i = 0; i < nCount; i++)
		{
			int nId;
			ar >> nId;
			localIdList.push_back(nId);
		}
		for (size_t i = 0; i < nCount; i++)
		{
			int nNum;
			ar >> nNum;
			localNumList.push_back(nNum);
		}
		for (size_t i = 0; i < nCount; i++)
		{
			double k;
			ar >> k;
			localKList.push_back(k);
		}
		ar >> localCoef >> insulThk >> blackness >> insulName >> city >> nBracketNum >> nPlaceId
			>> bracketLength >> bracketHeight >> windV >> ta >> nPipeId >> nWeldingType
			>> allowStress >> weldingRTPercent >> weldingCoef >> c1 >> c2 >> c3;

		ar >> ncomp;
		for (int i = 0; i < ncomp; i++)
		{
			CString str;
			ar >> str;
			int nLen = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
			WideCharToMultiByte(CP_ACP, 0, str, -1, components[i], refpropcharlength, NULL, NULL);
		}
		for (int i = 0; i < ncomp; i++)
		{
			ar >> x[i];
		}
	}
}

void CPipe::Run()
{
	if (0 == ncomp)
	{
		AfxMessageBox(L"未选择组分");
		return;
	}

	CString strComponent;
	for (int i = 0; i < ncomp; i++)
	{
		for (int j = 0; j < 121; j++)
		{
			if (strcmp(components[i], componentList[j][1]) == 0)
			{
				strComponent += CString(componentList[j][0]);
				if (i != ncomp - 1)
				{
					strComponent += _T(",");
				}	
			}
		}
	}
	strComponent += _T("(摩尔比例：");
	for (int i = 0; i < ncomp; i++)
	{
		CString strComposition;
		if (1 != ncomp)
		{
			strComposition.Format(_T("%f,"), x[i]);
			strComponent += strComposition;
		}
		else
		{
			strComposition.Format(_T("%f"), x[i]);
			strComponent += strComposition;
		}
	}
	strComponent += _T(")");

	int nLen = WideCharToMultiByte(CP_ACP, 0, city, -1, NULL, 0, NULL, NULL);
	char* chCity = new char[nLen];
	WideCharToMultiByte(CP_ACP, 0, city, -1, chCity, nLen, NULL, NULL);

	SetCurrentCtyw(chCity);
	delete[] chCity;

	SetCurrentPmp(nPipeId);

	CString sT, sP, sFlowRate, sTd, sPd, sDi, sDo, sLength, sRough, sInsulThk, sBlackness, sBracketNum, sBracketLength, sBracketHeight,
		sTa, sWindV, sAllowStress, sRTPercent, sC1, sC2, sC3;
	CString sPipeMaterial, sPipeCode, sWeldingType, sPlace, sShell;
	sT.Format(_T("%g"), t);
	sP.Format(_T("%g"), p);
	sFlowRate.Format(_T("%g"), flowRate);
	sTd.Format(_T("%g"), td);
	sPd.Format(_T("%g"), pd);
	sDi.Format(_T("%g"), di);
	sDo.Format(_T("%g"), dout);
	sLength.Format(_T("%g"), length);
	sRough.Format(_T("%g"), rough);
	sInsulThk.Format(_T("%g"), insulThk);
	sBlackness.Format(_T("%g"), blackness);
	sBracketNum.Format(_T("%d"), nBracketNum);
	sBracketLength.Format(_T("%g"), bracketLength);
	sBracketHeight.Format(_T("%g"), bracketHeight);
	sTa.Format(_T("%g"), ta);
	sWindV.Format(_T("%g"), windV);
	sAllowStress.Format(_T("%g"), allowStress);
	sRTPercent.Format(_T("%g"), weldingRTPercent);
	sC1.Format(_T("%g"), c1);
	sC2.Format(_T("%g"), c2);
	sC3.Format(_T("%g"), c3);
	
	if (pPmp != NULL)
	{
		sPipeMaterial = CString(pPmp->pipeName) + " " + CString(pPmp->MCode);
		sPipeCode = CString(pPmp->pipeCode);
	}
	
	switch (nWeldingType)
	{
	case 0:
		sWeldingType = L"单面对接焊";
		break;
	case 1:
		sWeldingType = L"双面对接焊";
		break;
	default:
		break;
	}

	switch (nPlaceId)
	{
	case 0:
		sPlace = L"室外";
		break;
	case 1:
		sPlace = L"室内";
		break;
	case 2:
		sPlace = L"地沟";
		break;
	default:
		break;
	}

	sShell = CString(Shell[nShellId]);

	if (flowRate < 1e-6)
	{
		AfxMessageBox(L"流量不应<=0");
		return;
	}

	//Water w_begin, w_end;
	//w_begin.SetStateTPX(t + 273.15, p + 0.101325, Water::TPAUTO);

	HINSTANCE RefpropdllInstance;
	RefpropdllInstance = LoadLibrary(CA2T(GetFullFileName("./prop.dll")));

	if (RefpropdllInstance == NULL)
	{
		AfxMessageBox(L"找不到prop.dll，请重新安装。");
		return;
	}
		
	fp_SETPATHdllTYPE SETPATHdll = (fp_SETPATHdllTYPE)GetProcAddress(RefpropdllInstance, "SETPATHdll");
	fp_SETUPdllTYPE SETUPdll = (fp_SETUPdllTYPE)GetProcAddress(RefpropdllInstance, "SETUPdll");
	fp_TPFLSHdllTYPE TPFLSHdll = (fp_TPFLSHdllTYPE)GetProcAddress(RefpropdllInstance, "TPFLSHdll");
	fp_PHFLSHdllTYPE PHFLSHdll = (fp_PHFLSHdllTYPE)GetProcAddress(RefpropdllInstance, "PHFLSHdll");
	fp_WMOLdllTYPE WMOLdll = (fp_WMOLdllTYPE)GetProcAddress(RefpropdllInstance, "WMOLdll");
	fp_TRNPRPdllTYPE TRNPRPdll = (fp_TRNPRPdllTYPE)GetProcAddress(RefpropdllInstance, "TRNPRPdll");

	long ierr = 0;
	char hf[refpropcharlength*ncmax], hrf[lengthofreference + 1],
		herr[errormessagelength + 1], hfmix[refpropcharlength + 1];
	char hpth[refpropcharlength];
	
	strcpy_s(hpth, sizeof hpth / sizeof hpth[0], GetExePath());
	SETPATHdll(hpth, refpropcharlength);
	
	for (int i = 0; i < ncomp; i++)
	{
		if (i == 0)
		{
			strcpy_s(hf, sizeof hf / sizeof hf[0], components[i]);
		}
		else
		{
			strcat_s(hf, sizeof hf / sizeof hf[0], "|");
			strcat_s(hf, sizeof hf / sizeof hf[0], components[i]);
		}
		//strcat_s(hf, sizeof hf / sizeof hf[0], ".FLD");
	}
	strcpy_s(hfmix, sizeof hfmix / sizeof hfmix[0], "hmx.bnc");
	strcpy_s(hrf, sizeof hrf / sizeof hrf[0], "DEF");
	strcpy_s(herr, sizeof herr / sizeof herr[0], "Ok");
	SETUPdll(ncomp, hf, hfmix, hrf, ierr, herr, refpropcharlength*ncmax, refpropcharlength,
		lengthofreference, errormessagelength);
	if (ierr != 0)
	{
		AfxMessageBox(CA2T(herr));
		return;
	}

	double _t_be, _p_be, d_be, dl_be, dv_be, q_be, e_be, h_be, s_be, cv_be, cp_be, w_be, xliq_be[ncmax], xvap_be[ncmax];
	_t_be = t + 273.15;
	_p_be = p * 1000 + 101.325;
	//x[0] = 1;
	TPFLSHdll(_t_be, _p_be, x, d_be, dl_be, dv_be, xliq_be, xvap_be, q_be, e_be, h_be, s_be, cv_be, cp_be, w_be, ierr, herr, errormessagelength);
	if (ierr != 0)
	{
		AfxMessageBox(CA2T(herr));
		return;
	}

	double M;
	WMOLdll(x, M);
	double dmass_be = d_be * M;// g/L
	double hmass_be = h_be / M; // J/g
	
	double eta_be, tcx_be;
	TRNPRPdll(_t_be, d_be, x, eta_be, tcx_be, ierr, herr, errormessagelength);
	if (ierr != 0)
	{
		AfxMessageBox(CA2T(herr));
		return;
	}
	
	
	double pipeD1 = (dout + insulThk * 2) / 1000;
	double dt, dp = PipeDpDt(components, x, ncomp, flowRate, t, p + 0.101325, di / 1000, dout / 1000, pipeD1,
		length, rough / 1000, localCoef, nInsulId, blackness, bracketLength / 1000,
		bracketHeight / 1000, nBracketNum, &dt);


	double ta = Ta(), wv = SpeedOfWind(), t0 = t - 0.5 * dt;;
	double ts, tm = Tm(dout / 1000, pipeD1, t0, nInsulId, blackness, ta, wv, &ts);
	double tc = Tc(nInsulId, tm);
	double shtc = Shtc(pipeD1, ts, ta, wv, blackness);
	double _Q = Q(dout / 1000, pipeD1, t0, ta, tc, shtc);
	double _q = q(pipeD1, _Q);
	double dQp = _q * length;
	double dQb = 0;
	if (bracketHeight > 1 && bracketLength > 1)
	{
		dQb = PipeBracketDq(bracketLength / 1000, bracketHeight / 1000, t0, ta, insulThk / 1000) * nBracketNum;
	}
	double dq = dQp + dQb;
	double p_en = 1000 * (p + 0.101325 - dp);
	double h_en = (hmass_be - dq / 1000 / flowRate * 3600) * M;
	//w_end.SetStatePH(p + 0.101325 - dp, h_end);
	
	double t_en, d_en, dl_en, dv_en, q_en, e_en, s_en, cv_en, cp_en, w_en, xliq_en[ncmax], xvap_en[ncmax];
	PHFLSHdll(p_en, h_en, x, t_en, d_en, dl_en, dv_en, xliq_en, xvap_en, q_en, e_en, s_en, cv_en, cp_en, w_en, ierr, herr, errormessagelength);
	if (ierr != 0)
	{
		AfxMessageBox(CA2T(herr));
		return;
	}

	double eta_en, tcx_en;
	TRNPRPdll(t_en, d_en, x, eta_en, tcx_en, ierr, herr, errormessagelength);
	if (ierr != 0)
	{
		AfxMessageBox(CA2T(herr));
		return;
	}

	FreeLibrary(RefpropdllInstance);

	double dmass_en = d_en * M;// g/L
	double hmass_en = h_en / M; // J/g
	double averageDensity = 0.5 * (dmass_be + dmass_en);
	double averageViscosity = 0.5 * (eta_be / 1e6 + eta_en / 1e6);
	double averageVelocity = FlowVelocity(flowRate, averageDensity, di / 1000);
	double re = Re(averageVelocity, averageDensity, averageViscosity, di / 1000);
	double frictionalCoef = FrictionalCoef(di / 1000, re, rough / 1000);
	double sdp = StraightPipeDp(averageDensity, averageVelocity, di / 1000, length, frictionalCoef);
	double ldp = LocalPipeDp(averageDensity, averageVelocity, localCoef);
	
	
	//int nLen = WideCharToMultiByte(CP_ACP, 0, m_insulName, -1, NULL, 0, NULL, NULL);
	//char* chInsulName = new char[nLen];
	//WideCharToMultiByte(CP_ACP, 0, m_insulName, -1, chInsulName, nLen, NULL, NULL);

	double y;
	if (pPmp != NULL)
	{
		y = PipeThkY(pPmp->ShapeCode, td);
	} 

	double thk = PipeThickness(dout, pd, allowStress, y, weldingCoef, 1, c1 / 100, c2, c3);

	CString sDensity_A, sEnthalpy_A, sViscosity_A, sT_B, sP_B, sDensity_B, sEnthalpy_B, sViscosity_B,
		sAverageVelocity, sFriction, sRe, sSdP, sLdP, sDP,
		sTc, sShtc, sAverageT, sTs, sq_length, sQ_Area, sq_bracket, sQsum,
		sThk_clac, sThk_sel;

	sDensity_A.Format(_T("%.2f"), dmass_be);
	sEnthalpy_A.Format(_T("%.2f"), hmass_be);
	sViscosity_A.Format(_T("%.2e"), eta_be / 1e6);

	sT_B.Format(_T("%.2f"), t_en - 273.15);
	sP_B.Format(_T("%.2f"), p - dp);
	sDensity_B.Format(_T("%.2f"), dmass_en);
	sEnthalpy_B.Format(_T("%.2f"), hmass_en);
	sViscosity_B.Format(_T("%.2e"), eta_en / 1e6);
	sAverageVelocity.Format(_T("%.2f"), averageVelocity);
	sFriction.Format(_T("%.4f"), 1 / frictionalCoef);
	sRe.Format(_T("%.2f"), re);
	sSdP.Format(_T("%.6f"), sdp);
	sLdP.Format(_T("%.6f"), ldp);
	sDP.Format(_T("%.6f"), dp);
	sTc.Format(_T("%.4f"), tc);
	sShtc.Format(_T("%.4f"), shtc);
	sAverageT.Format(_T("%.2f"), t0);
	sTs.Format(_T("%.2f"), ts);
	sq_length.Format(_T("%.2f"), _q);
	sQ_Area.Format(_T("%.2f"), _Q);
	sq_bracket.Format(_T("%.2f"), dQb);
	sQsum.Format(_T("%.2f"), dq);
	sThk_clac.Format(_T("%.2f"), thk);
	sThk_sel.Format(_T("%.2f"), 0.5 * (dout - di));
	
	
	_Application word_app;
	if (!word_app.CreateDispatch(_T("Word.Application"), NULL))//这是office环境
	//if (!word_app.CreateDispatch(_T("KWPS.Application"), NULL))//这是wps环境
	{
		AfxMessageBox(_T("本机没有安装word产品！"));
		return;
	}
	//word_app.SetVisible(FALSE);//设置word不可见
	word_app.SetVisible(TRUE);//设置word可见

	//新建文档
	COleVariant vTrue((short)TRUE), vFalse((short)FALSE), VOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
	Documents docs;
	_Document doc;
	Bookmarks bookmarks;
	Bookmark bookmark;
	Range range;
	docs = word_app.GetDocuments();

	CString path(GetExePath());
	path += "\\templ.dot";

	doc = docs.Add(new CComVariant(path), new CComVariant(false), new CComVariant(0), new CComVariant());

	bookmarks = doc.GetBookmarks();

	bookmark = bookmarks.Item(&_variant_t(_T("COMPONENT")));
	range = bookmark.GetRange();
	range.SetText(strComponent);

	bookmark = bookmarks.Item(&_variant_t(_T("T")));
	range = bookmark.GetRange();
	range.SetText(sT);

	bookmark = bookmarks.Item(&_variant_t(_T("P")));
	range = bookmark.GetRange();
	range.SetText(sP);

	bookmark = bookmarks.Item(&_variant_t(_T("FlowRate")));
	range = bookmark.GetRange();
	range.SetText(sFlowRate);

	bookmark = bookmarks.Item(&_variant_t(_T("Td")));
	range = bookmark.GetRange();
	range.SetText(sTd);

	bookmark = bookmarks.Item(&_variant_t(_T("Pd")));
	range = bookmark.GetRange();
	range.SetText(sPd);

	bookmark = bookmarks.Item(&_variant_t(_T("Di")));
	range = bookmark.GetRange();
	range.SetText(sDi);

	bookmark = bookmarks.Item(&_variant_t(_T("Do")));
	range = bookmark.GetRange();
	range.SetText(sDo);

	bookmark = bookmarks.Item(&_variant_t(_T("Length")));
	range = bookmark.GetRange();
	range.SetText(sLength);

	bookmark = bookmarks.Item(&_variant_t(_T("Pm")));
	range = bookmark.GetRange();
	range.SetText(sPipeMaterial);

	bookmark = bookmarks.Item(&_variant_t(_T("PipeCode")));
	range = bookmark.GetRange();
	range.SetText(sPipeCode);

	bookmark = bookmarks.Item(&_variant_t(_T("AllowStress")));
	range = bookmark.GetRange();
	range.SetText(sAllowStress);

	bookmark = bookmarks.Item(&_variant_t(_T("RtPercent")));
	range = bookmark.GetRange();
	range.SetText(sRTPercent);

	bookmark = bookmarks.Item(&_variant_t(_T("C1")));
	range = bookmark.GetRange();
	range.SetText(sC1);

	bookmark = bookmarks.Item(&_variant_t(_T("C2")));
	range = bookmark.GetRange();
	range.SetText(sC2);

	bookmark = bookmarks.Item(&_variant_t(_T("C3")));
	range = bookmark.GetRange();
	range.SetText(sC3);

	bookmark = bookmarks.Item(&_variant_t(_T("Rough")));
	range = bookmark.GetRange();
	range.SetText(sRough);

	bookmark = bookmarks.Item(&_variant_t(_T("Place")));
	range = bookmark.GetRange();
	range.SetText(sPlace);

	bookmark = bookmarks.Item(&_variant_t(_T("Insul")));
	range = bookmark.GetRange();
	range.SetText(insulName);

	bookmark = bookmarks.Item(&_variant_t(_T("Shell")));
	range = bookmark.GetRange();
	range.SetText(sShell);

	bookmark = bookmarks.Item(&_variant_t(_T("InsulThk")));
	range = bookmark.GetRange();
	range.SetText(sInsulThk);

	bookmark = bookmarks.Item(&_variant_t(_T("Blackness")));
	range = bookmark.GetRange();
	range.SetText(sBlackness);

	bookmark = bookmarks.Item(&_variant_t(_T("BracketNum")));
	range = bookmark.GetRange();
	range.SetText(sBracketNum);

	bookmark = bookmarks.Item(&_variant_t(_T("BracketLength")));
	range = bookmark.GetRange();
	range.SetText(sBracketLength);

	bookmark = bookmarks.Item(&_variant_t(_T("BracketHeight")));
	range = bookmark.GetRange();
	range.SetText(sShell);

	bookmark = bookmarks.Item(&_variant_t(_T("City")));
	range = bookmark.GetRange();
	range.SetText(city);

	bookmark = bookmarks.Item(&_variant_t(_T("Ta")));
	range = bookmark.GetRange();
	range.SetText(sTa);

	bookmark = bookmarks.Item(&_variant_t(_T("WindV")));
	range = bookmark.GetRange();
	range.SetText(sWindV);


	size_t nSize = localIdList.size();
	for (size_t i = 0; i < nSize; i++)
	{
		CString sLocalNumBookmark, sLocalNum, sLocalKBookmark, sLocalK;
		sLocalNumBookmark.Format(_T("LocalNum%d"), localIdList[i]);
		sLocalNum.Format(_T("%d"), localNumList[i]);

		sLocalKBookmark.Format(_T("LocalK%d"), localIdList[i]);
		sLocalK.Format(_T("%g"), localKList[i]);

		bookmark = bookmarks.Item(&_variant_t(sLocalNumBookmark));
		range = bookmark.GetRange();
		range.SetText(sLocalNum);

		bookmark = bookmarks.Item(&_variant_t(sLocalKBookmark));
		range = bookmark.GetRange();
		range.SetText(sLocalK);
	}


	bookmark = bookmarks.Item(&_variant_t(_T("T_A")));
	range = bookmark.GetRange();
	range.SetText(sT);

	bookmark = bookmarks.Item(&_variant_t(_T("P_A")));
	range = bookmark.GetRange();
	range.SetText(sP);

	bookmark = bookmarks.Item(&_variant_t(_T("Density_A")));
	range = bookmark.GetRange();
	range.SetText(sDensity_A);

	bookmark = bookmarks.Item(&_variant_t(_T("Enthalpy_A")));
	range = bookmark.GetRange();
	range.SetText(sEnthalpy_A);

	bookmark = bookmarks.Item(&_variant_t(_T("Viscosity_A")));
	range = bookmark.GetRange();
	range.SetText(sViscosity_A);

	bookmark = bookmarks.Item(&_variant_t(_T("T_B")));
	range = bookmark.GetRange();
	range.SetText(sT_B);

	bookmark = bookmarks.Item(&_variant_t(_T("P_B")));
	range = bookmark.GetRange();
	range.SetText(sP_B);

	bookmark = bookmarks.Item(&_variant_t(_T("Density_B")));
	range = bookmark.GetRange();
	range.SetText(sDensity_B);

	bookmark = bookmarks.Item(&_variant_t(_T("Enthalpy_B")));
	range = bookmark.GetRange();
	range.SetText(sEnthalpy_B);

	bookmark = bookmarks.Item(&_variant_t(_T("Viscosity_B")));
	range = bookmark.GetRange();
	range.SetText(sViscosity_B);

	bookmark = bookmarks.Item(&_variant_t(_T("Velocity")));
	range = bookmark.GetRange();
	range.SetText(sAverageVelocity);

	bookmark = bookmarks.Item(&_variant_t(_T("Friction")));
	range = bookmark.GetRange();
	range.SetText(sFriction);

	bookmark = bookmarks.Item(&_variant_t(_T("Re")));
	range = bookmark.GetRange();
	range.SetText(sRe);

	bookmark = bookmarks.Item(&_variant_t(_T("SdP")));
	range = bookmark.GetRange();
	range.SetText(sSdP);

	bookmark = bookmarks.Item(&_variant_t(_T("LdP")));
	range = bookmark.GetRange();
	range.SetText(sLdP);

	bookmark = bookmarks.Item(&_variant_t(_T("DP")));
	range = bookmark.GetRange();
	range.SetText(sDP);

	bookmark = bookmarks.Item(&_variant_t(_T("Tc")));
	range = bookmark.GetRange();
	range.SetText(sTc);

	bookmark = bookmarks.Item(&_variant_t(_T("Shtc")));
	range = bookmark.GetRange();
	range.SetText(sShtc);

	bookmark = bookmarks.Item(&_variant_t(_T("AverageT")));
	range = bookmark.GetRange();
	range.SetText(sAverageT);

	bookmark = bookmarks.Item(&_variant_t(_T("Ts")));
	range = bookmark.GetRange();
	range.SetText(sTs);

	bookmark = bookmarks.Item(&_variant_t(_T("ql")));
	range = bookmark.GetRange();
	range.SetText(sq_length);

	bookmark = bookmarks.Item(&_variant_t(_T("Q_Area")));
	range = bookmark.GetRange();
	range.SetText(sQ_Area);

	bookmark = bookmarks.Item(&_variant_t(_T("qb")));
	range = bookmark.GetRange();
	range.SetText(sq_bracket);

	bookmark = bookmarks.Item(&_variant_t(_T("Q")));
	range = bookmark.GetRange();
	range.SetText(sQsum);

	bookmark = bookmarks.Item(&_variant_t(_T("Thk_clac")));
	range = bookmark.GetRange();
	range.SetText(sThk_clac);

	bookmark = bookmarks.Item(&_variant_t(_T("Thk_sel")));
	range = bookmark.GetRange();
	range.SetText(sThk_sel);


	//doc.Close(vFalse, VOpt, VOpt);
	doc.ReleaseDispatch();
	docs.ReleaseDispatch();
	//word_app.Quit(VOpt, VOpt, VOpt);
	word_app.ReleaseDispatch();
}